package com.lvmama.vst.controller;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lvmama.vst.dao.UserEntity;
import com.lvmama.vst.dto.UserResponse;
import com.lvmama.vst.service.UserService;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	UserService userService;
	
	@ApiOperation("Get all users")
	@RequestMapping("")
	public ResponseEntity<Collection<UserEntity>> getUsers(){
		return new ResponseEntity<>(this.userService.getAllUser(), HttpStatus.OK);
	}
	
	@ApiOperation("Get one user by user id")
	@RequestMapping(method = RequestMethod.GET,
	value = "/{id}",
	produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity< UserEntity> getUserById(@PathVariable("id") String id){
		return new ResponseEntity<>(this.userService.getUserById(id), HttpStatus.OK);
	}
	
	@ApiOperation("Save one user")
	@RequestMapping(method = RequestMethod.POST, 
					value = "",
					produces = MediaType.APPLICATION_JSON_VALUE, 
					consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<UserResponse> addUser(@Valid @RequestBody UserEntity user) {
		try {
			this.userService.addUser(user);
			UserResponse response = new UserResponse(0, "success"); 
			return new ResponseEntity<>(response, HttpStatus.OK);
		}
		catch (Exception ex) {
			
		}
		return null;
	}
}
 